﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using DevExpress.XtraPivotGrid;

namespace ReportingManager
{
    public partial class frmKolonAyar : DevExpress.XtraEditors.XtraForm
    {
        public frmKolonAyar()
        {
            InitializeComponent();
        }

        #region Properties
        List<TableInfo> tablePropertie = new List<TableInfo>();
        public object DataSource { get; set; }
        public string SqlQuery { get; set; }
        #endregion

        private void frmKolonAyar_Load(object sender, EventArgs e)
        {
            this.DataSource = Database.DataSender(this.SqlQuery);
            //Eğer Dosya Var ise Dosyadan açmak için önce sor eğer adam kabul ederse var olan xml dosyasını aç kabul etmez ise yeniden olustur
            if (File.Exists(Config.Get.Path + "\\" + Config.Get.RaporAdi + ".xml"))
                //if ((MessageBox.Show("Daha Önceden Kayıtlı Dosyanız Bulunmaktadır.\r\nVarolan Dosyayı Açmak için Tamam Tuşuna Basın", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes))
                CreateTableInfo();
            else
                LoadSource();

            grdControl.DataSource = tablePropertie.ToArray();
            grdViewTable.Columns[0].OptionsColumn.AllowEdit = false;
            grdViewTable.Columns[0].Caption = "Alan Adı";
            grdViewTable.Columns[1].Caption = "Satırda Gör";
            grdViewTable.Columns[2].Caption = "Filtre Uygula";
            grdViewTable.Columns[3].Caption = "Datayı Bağla";
            grdViewTable.Columns[4].Caption = "Kolonda Gör";
            grdViewTable.Columns[5].Caption = "Hiçbiri";
            cmbGroupInterval.Items.AddRange(Enum.GetNames(typeof(PivotGroupInterval)));
            grdViewTable.Columns[6].ColumnEdit = cmbGroupInterval;
            grdViewTable.Columns[6].Caption = "Tarih Biçimi";
            grdViewTable.Columns[7].Visible = false;
            grdViewTable.Columns[6].OptionsColumn.AllowEdit = false;
        }




        #region Metodlar
        void CreateTableInfo()
        {
            try
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Config.Get.Path + "\\" + Config.Get.RaporAdi + ".xml");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    tablePropertie.Add(new TableInfo() { Kolon_Adi = ds.Tables[0].Rows[i][0].ToString(), Filtre_Uygula = Convert.ToBoolean(ds.Tables[0].Rows[i][1]), Datada_Gor = Convert.ToBoolean(ds.Tables[0].Rows[i][2]), Kolonda_Gor = Convert.ToBoolean(ds.Tables[0].Rows[i][3]), Hicbiri = Convert.ToBoolean(ds.Tables[0].Rows[i][4]), Satirda_Gor = Convert.ToBoolean(ds.Tables[0].Rows[i][5]), GroupInterval = (PivotGroupInterval)Convert.ToByte(ds.Tables[0].Rows[i][6]), DbType = Type.GetType(ds.Tables[0].Rows[i][7].ToString()) });
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void LoadSource()
        {
            if ((this.DataSource != null) && (this.DataSource is DataTable))
            {
                for (int i = 0; i < (this.DataSource as DataTable).Columns.Count; i++)
                {
                    tablePropertie.Add(new TableInfo() { Kolon_Adi = (this.DataSource as DataTable).Columns[i].ColumnName, Datada_Gor = false, Filtre_Uygula = false, Hicbiri = false, Kolonda_Gor = false, Satirda_Gor = false, GroupInterval = PivotGroupInterval.Default, DbType = (this.DataSource as DataTable).Columns[i].DataType });
                }
            }
        }
        void CreateXmlDocument()
        {
            try
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.TableName = "AlanAdlari";
                dt.Columns.Add("Kolon_Adi", typeof(string));
                dt.Columns.Add("Filtre_Uygula", typeof(bool));
                dt.Columns.Add("Datada_Gor", typeof(bool));
                dt.Columns.Add("Kolonda_Gor", typeof(bool));
                dt.Columns.Add("Hicbiri", typeof(bool));
                dt.Columns.Add("SatirdaGoster", typeof(bool));
                dt.Columns.Add("GroupInterval", typeof(PivotGroupInterval));
                dt.Columns.Add("DbType", typeof(string));
                for (int i = 0; i < tablePropertie.Count; i++)
                {
                    dt.Rows.Add(tablePropertie[i].Kolon_Adi, tablePropertie[i].Filtre_Uygula, tablePropertie[i].Datada_Gor, tablePropertie[i].Kolonda_Gor, tablePropertie[i].Hicbiri, tablePropertie[i].Satirda_Gor, tablePropertie[i].GroupInterval, tablePropertie[i].DbType.ToString());
                }
                ds.Tables.Add(dt);
                ds.WriteXml(Config.Get.Path + "\\" + Config.Get.RaporAdi + ".xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kayıt Yapılırken Bir Hata Oluştu :\r\n" + ex.Message);
            }

        }
        #endregion

        private void grdViewTable_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.AbsoluteIndex != 6 && Convert.ToBoolean(e.Value))
            {
                if (e.Column.Name == "colHicbiri")
                {
                    grdViewTable.SetRowCellValue(grdViewTable.FocusedRowHandle, grdViewTable.Columns[1], false);
                    grdViewTable.SetRowCellValue(grdViewTable.FocusedRowHandle, grdViewTable.Columns[2], false);
                    grdViewTable.SetRowCellValue(grdViewTable.FocusedRowHandle, grdViewTable.Columns[3], false);
                    grdViewTable.SetRowCellValue(grdViewTable.FocusedRowHandle, grdViewTable.Columns[4], false);
                }
                else
                {
                    grdViewTable.SetRowCellValue(grdViewTable.FocusedRowHandle, grdViewTable.Columns[5], false);
                }
            }
        }

        private void grdViewTable_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (grdViewTable.Columns.Count > 0)
                {
                    if (tablePropertie[grdViewTable.FocusedRowHandle].DbType != typeof(DateTime))
                        grdViewTable.Columns[6].OptionsColumn.AllowEdit = false;
                    else if (grdViewTable.Columns[6] != null)
                        grdViewTable.Columns[6].OptionsColumn.AllowEdit = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Programda Bir Hata Oluştu!.." + ex.Message);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                CreateXmlDocument();

                frmRaporGoruntule frm = new frmRaporGoruntule();
                frm.DataSource = this.DataSource;
                frm.Show();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}